<?php
/**
 * Created by PhpStorm.
 * User: rain
 * Date: 2018/4/5
 * Time: 下午4:51
 */

namespace App\Http\Controllers\Api;


use App\Models\Banner;
use App\Models\ExamDate;
use App\Utils\FileUtil;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\Storage;
use Ramsey\Uuid\Uuid;


class BannerController extends AuthorizedController
{
    public function upload(Request $request)
    {

        $urls = [];

        try{
            foreach($request->file() as $file){
                $fileName = 'book/chapter/' . Uuid::uuid1() . '.jpg';
                $disk = Storage::disk('oss');
                $disk->put($fileName, file_get_contents($file->getRealPath()));
                $urls[] = FileUtil::getDomain($fileName);
            }

            return [
                "errno" => 0,
                "data" => $urls,
            ];
        }catch(\Exception $e){
            return [
                'msg' => $e->getMessage(),
                "errno" => 0,
                "data" => [],
            ];
        }


    }


    public function index()
    {
        return $this->success([
            'banners' => Banner::getPositionBanners($this->getCity()),
            'count_down' => ExamDate::getCountDown($this->getCity()),
            'version' => env('APP_VERSION', '1.0')
        ]);
    }

    public function banner(Request $request)
    {
        $postion = $request->get('position', 0);

        return $this->success(['banners' => Banner::getPositionBanners($this->getCity())]);
    }


}
